<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="公司名称" prop="itName">
				<el-input v-model="queryParams.itName" placeholder="请输入公司名称" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="公司地址" prop="itAddress">
				<el-input v-model="queryParams.itAddress" placeholder="请输入公司地址" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="话题" prop="itTopic">
				<el-select v-model="queryParams.itTopic" placeholder="请选择">
					<el-option label="全部" value="">
					</el-option>
					<el-option v-for="(item,i) in itTopicList" :key="i" :label="item.itTopic" :value="item.itId">
					</el-option>
				</el-select>
				<!-- <el-input v-model="queryParams.itTopic" placeholder="请输入话题" clearable size="small"
					@keyup.enter.native="handleQuery" /> -->
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['mini:company:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['mini:company:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['mini:company:remove']">删除</el-button>
			</el-col>
			<!--    <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mini:company:export']"
        >导出</el-button>
      </el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="公司名称" align="center" prop="itName" />
			<el-table-column label="公司地址" align="center" prop="itAddress" >
				<template slot-scope="scope">
					<span class="spanContent" :title="scope.row.itAddress">
						{{scope.row.itAddress}}
					</span>
				</template>
			</el-table-column>
			<el-table-column label="内容" align="center" prop="itContent">
				<template slot-scope="scope">
					<span class="spanContent" :title="scope.row.itContent">
						{{scope.row.itContent.replace(/<[^>]+>/g, ' ')}}
					</span>
				</template>
			</el-table-column>
			<el-table-column label="话题" align="center" prop="itTopic" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['mini:company:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['mini:company:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改公司基本信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="公司名称" prop="itName">
					<el-input v-model="form.itName" placeholder="请输入公司名称" />
				</el-form-item>
				<el-form-item label="公司地址" prop="itAddress">
					<el-input v-model="form.itAddress" placeholder="请输入公司地址" />
				</el-form-item>
				<el-form-item label="内容">
					<editor v-model="form.itContent" :min-height="192" />
				</el-form-item>
				<el-form-item label="图片">
					<el-upload action="" list-type="picture-card" :on-remove="handleRemove"
						:before-upload="handleBeforeUpload" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="话题" prop="itSubjectId">
					<el-select v-model="form.itSubjectId" placeholder="请选择">
						<el-option label="全部" value="">
						</el-option>
						<el-option v-for="(item,i) in itTopicList" :key="i" :label="item.itTopic" :value="item.itId">
						</el-option>
					</el-select>
					<!-- <el-input v-model="form.itTopic" placeholder="请输入话题" /> -->
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listCompany,
		getCompany,
		delCompany,
		addCompany,
		updateCompany,
		uploadImage,
	} from "@/api/mini/company";
	import {
		listTopic
	} from "@/api/mini/topic";

	export default {
		name: "Company",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 公司基本信息表格数据
				companyList: [],
				//话题列表
				itTopicList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					itName: null,
					itAddress: null,
					itContent: null,
					itTopic: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				//上传文件展示列表
				fileList: [],
				//上传文件列表
				filePathList: [],
			};
		},
		created() {
			this.getList();
			this.getTopicList();
		},
		methods: {
			// http://localhost:8080//mini/company/avatar
			handleBeforeUpload(file) {
				if (this.fileList && this.fileList.length >= 5) {
					this.$modal.msgWarning("公司图片只能上传5张！");
					return false;
				}
				let formData = new FormData();
				formData.append("avatarfile", file);
				uploadImage(formData).then(response => {
					let pathList = response.data.realPath.split('/')
					this.fileList.push({
						name: pathList[pathList.length - 1],
						url: response.data.realPath
					})
					this.filePathList.push(response.data.filePath)
				});
				return false;
			},
			handleRemove(file, fileList) {
				let index = this.fileList.findIndex(item => item.name == file.name);
				console.log("12121212",index);
				if (index >= 0) {
					this.fileList.splice(index, 1);
					 this.filePathList.splice(index, 1);
				}
				console.log(this.filePathList, this.fileList, file, fileList);
			},
			/** 查询公司基本信息列表 */
			getList() {
				this.loading = true;
				listCompany(this.queryParams).then(response => {
					this.companyList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询话题列表 */
			getTopicList() {
				listTopic({}).then(response => {
					this.itTopicList = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					itName: null,
					itAddress: null,
					itContent: null,
					itSubjectId: null,
				};
				this.fileList = []
				this.filePathList = []
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.itId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加公司基本信息";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const itId = row.itId || this.ids
				getCompany(itId).then(response => {
					console.log(response.data);
					this.form = response.data;
					this.filePathList = response.data.itImg.split(",")
					this.fileList = this.filePathList.map(item => {
						let pathList = item.split('/')
						return {
							name: pathList[pathList.length - 1],
							url: response.data.filePath+item,
						}
					})
					this.open = true;
					this.title = "修改公司基本信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.itId != null) {
							updateCompany({
								...this.form,
								itImg: this.filePathList.join(",")
							}).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addCompany({
								...this.form,
								itImg: this.filePathList.join(",")
							}).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const itIds = row.itId || this.ids;
				const itName = row.itName || this.itName;
				let confirmStr = '是否确认删除公司:"' + itName + '"？';
				if (!itName) {

					confirmStr = '是否确认删除所选公司？';
				}
				this.$modal.confirm(confirmStr).then(function() {
					return delCompany(itIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('mini/company/export', {
					...this.queryParams
				}, `company_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
<style type="text/css" scoped>
	.spanContent {
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
		width: 100%
	}
</style>
